package com.zw.pm.module.system.log;

import com.zw.pm.gen.entity.Log;
import com.zw.pm.gen.entity.User;
import com.zw.pm.gen.mapper.LogMapper;
import com.zw.pm.util.CompareUtil;
import com.zw.pm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 日志服务
 *
 * @Author: China.sgl
 * @Date: 2019/11/28 09:11
 */
@Service
public class LogService {

    @Autowired
    private LogMapper logMapper;
    @Autowired
    private LogDao logDao;


    /**
     * 查询日志
     *
     * @param log 条件
     * @return list
     */
    public List<LogModel> findAll(LogModel log) {
        return logDao.findAll(log);
    }

    /**
     * 保存日志
     *
     * @param log 日志类
     * @return 影响行数
     */
    public int saveLog(Log log) {
        return logMapper.insert(log);
    }

    /**
     * 保存日志
     *
     * @param operationName 操作名
     * @param operationContent 操作内容
     * @return 影响行数
     */
    public int saveLog(String operationName, String operationContent) {
        User user = ShiroUtils.getUser();
        Log log = new Log();
        log.setAccount(user.getAccount());
        log.setName(user.getName());
        log.setOperationName(operationName);
        log.setOperationTime(new Date());
        log.setOperationContent(operationContent);
        log.setAddress(ShiroUtils.getIp());
        return saveLog(log);
    }

    /**
     * 保存日志
     *
     * @param operationName 操作名
     * @param before 源对象
     * @param after 目标对象
     * @return 影响行数
     */
    public int saveLog(String operationName, Object before, Object after) {
        return saveLog(operationName, CompareUtil.compare(before, after));
    }
}
